<template>
  <div class="app-container">
    <div class="filter-container" style="margin-bottom:20px">
      <span style="margin-left:10px">标签名称：</span>
      <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="请输入标签名称" v-model="listQuery.labelName">
      </el-input>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>
      <el-button type="primary" style="margin-left: 10px;" @click="handleCreate">新增标签</el-button>
    </div>
    <el-table :data="list" v-loading.body="listLoading" element-loading-text="Loading" border fit highlight-current-row>
      <el-table-column align="center" label='标签ID' min-width="110" prop="id"></el-table-column>
      <el-table-column class-name="status-col" label="标签名称" min-width="110" align="center" prop="name"></el-table-column>
      <el-table-column label="权重" min-width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.sort }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" min-width="110">
        <template slot-scope="scope">
          <el-button size="small" @click="handleUpdate(scope.row)">编辑
          </el-button>
          <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div v-show="!listLoading" class="pagination-container">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.pageNo"
        :page-sizes="[10,20,30,50]" :page-size="listQuery.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
    <el-dialog :title="dialogStatus=='create' ? '新增标签' : '编辑标签'" :visible.sync="dialogFormVisible" center>
      <el-form :model="form" :rules="rules" ref="ruleForm">
        <el-form-item label="标签名称" label-width="80px" prop="name">
          <el-input v-model="form.name" auto-complete="off" maxlength="5"
          placeholder="请输入标签名称（不超过5个字）"></el-input>
        </el-form-item>
        <el-form-item label="权重" label-width="80px" prop="sort">
          <el-input-number  v-model="form.sort" :step="1" :min="0"></el-input-number>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="addForm">确 定</el-button>
        <el-button v-else type="primary" @click="updateForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import { get, add, update, delId } from '@/api/labels'

export default {
  data() {
    return {
      dialogStatus:'create',
      form:{
        id:0,
        name:'',
        sort:1
      },
      dialogFormVisible:false,
      list: null,
      total:null,
      listQuery:{
        labelName:'',
        pageNo:1,
        pageSize:10
      },
      listLoading: true,
      rules: {
        name: [{ required: true, message: '请输入标签名称', trigger: 'blur' }],
        sort: [{ required: true, message: '请输入权重', trigger: 'blur' }]
      }
    }
  },
  mounted() {
    this.fetchData()
  },
  methods: {
    handleDelete(idx, row) {
      const { id } = row
      delId(id).then(response => {
        const { code } = response
        if (code == 0) {
          this.$notify({
            title: '成功',
            message: '删除资讯标签成功',
            type: 'success',
            duration: 2000
          })
          const index = this.list.indexOf(idx)
          this.list.splice(idx, 1)
          this.total--
        } else {
          this.$notify({
            title: '失败',
            message: '删除资讯标签失败',
            type: 'error',
            duration: 2000
          })
        }
      })
    },
    handleFilter() {
      this.listQuery.pageNo = 1
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.listQuery.pageNo = val
      this.fetchData()
    },
    handleSizeChange(val) {
      this.listQuery.pageSize = val
      this.fetchData()
    },
    handleCreate() {
      const { list } = this
      // if(list.length >= 20){
      //   this.$message('最多配置20个标签')
      //   return
      // }
      this.form = {
        id:0,
        name:'',
        sort:1
      }
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    handleUpdate(row) {
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.form = Object.assign({}, row)
      })
    },
    fetchData() {
      this.listLoading = true
      const { listQuery } = this
      get(listQuery).then(response => {
        this.list = response.data['list']
        this.total = response.data['total']
        this.listLoading = false
      })
    },
    updateForm(){
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          const { form } = this
          update(form).then(response => {
            const { code, message } = response
            if(code == 0){
              this.fetchData()
              this.dialogFormVisible = false
            }else{
              this.$message(message)
            }
          })
        } else {
          return false
        }
      })
    },
    addForm() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          const { form } = this
          add(form).then(response => {
            const { code, message } = response
            if(code == 0){
              this.fetchData()
              this.dialogFormVisible = false
            }else{
              this.$message(message)
            }
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
  .pagination-container{
    margin-top:30px;
  }
</style>
